﻿// -----------------------------------------------------------------------
// <copyright file="Equipment_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using PMSGSC.Common;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Equipment_DAL: BaseDAL
    {

        // Lấy tất cả Equipment
        public static List<Equipment> GetAll(int shipID)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Equipments);
            return DB.Equipments.Where(n => n.Ship_ID == shipID).ToList();
        }
        // Lấy Equipment theo ParentID
        public static List<Equipment> GetDataByParentId(int id)
        {
            return DB.Equipments.Where(e => e.ParentID == id).ToList();
        }

        public static Equipment GetDataByName(string name)
        {
            return DB.Equipments.Where(e => e.NameEquipment == name).FirstOrDefault();
        }

        // Lấy Equipment theo Id
        public static Equipment GetDataById(int id)
        {
            return DB.Equipments.Where(e => e.Equipment_ID == id).FirstOrDefault();
        }
        public static List<Equipment> GetCounterTypeEquipment(int id)
        {
            if (id != 0)
            {
                Equipment temp = DB.Equipments.Where(n=>n.Equipment_ID == id).FirstOrDefault();


                if(!temp.IsCounter.Value)
                     return DB.Equipments.Where(n => n.ParentID == id && n.IsCounter == true).ToList();
                else
                    return DB.Equipments.Where(n => n.Equipment_ID == id && n.IsCounter == true).ToList();
            }
            else
                return DB.Equipments.Where(n => n.IsCounter == true).ToList();
        }
        //Thêm một Equipment
        public static bool Insert(Equipment equipment)
        {
            try
            {
                DB.Equipments.InsertOnSubmit(equipment);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một Equipment
        public static bool Delete(Equipment equipment)
        {
            try
            {
                DB.Equipments.DeleteOnSubmit(equipment);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // Cập nhật Equipment
        public static bool Update(Equipment equipment)
        {
            try
            {
                Equipment objEquipment = GetDataById(equipment.Equipment_ID);
                objEquipment.NameEquipment = equipment.NameEquipment;
                objEquipment.CodeEquipment = equipment.CodeEquipment;
                objEquipment.Maker_ID = equipment.Maker_ID;
                objEquipment.ParentID = equipment.ParentID;
                objEquipment.Details = equipment.Details;
                objEquipment.Serial = equipment.Serial;
                objEquipment.Ship_ID = equipment.Ship_ID;
                objEquipment.Vendor_ID = equipment.Vendor_ID;
                objEquipment.Status_ID = equipment.Status_ID;

                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool UpdateCounter(Equipment equip)
        {
            try
            {
                Equipment objEquipment = GetDataById(equip.Equipment_ID);
                objEquipment.CurrentCounter = equip.CurrentCounter;
                DB.SubmitChanges();
               
                return true;
            }
            catch {
                return false;
            }
        }

        public static bool TestBeforeDeleteComponent(int id)
        {
            Job objJob = DB.Jobs.Where(e => e.Equipment_ID == id).FirstOrDefault();
            if (objJob != null)
                return false;
            PartsInEquipment objPart = DB.PartsInEquipments.Where(e => e.Equipment_ID == id).FirstOrDefault();
            if (objPart != null)
                return false;
            History objHistory = DB.Histories.Where(e => e.Job.Equipment_ID == id).FirstOrDefault();
            if (objHistory != null)
                return false;
            Attachment objAttach = DB.Attachments.Where(a => a.Equipment_ID == id).FirstOrDefault();
            if (objAttach != null)
                return false;
            HistoryCounter hisCounter = DB.HistoryCounters.Where(h => h.Equipment_ID == id).FirstOrDefault();
            if (hisCounter != null)
                return false;

            return true;
        }

        static bool isFinded;
        public static bool TestBeforeDeleteComponentFinal(int id)
        {
            isFinded = true;
            if (!TestBeforeDeleteComponent(id))
                return false;

            return TestBeforeDeleteChildComponentFinal(id);
        }

        public static bool TestBeforeDeleteChildComponentFinal(int id)
        {
            List<Equipment> childComponentList = DB.Equipments.Where(e => e.ParentID == id).ToList();
            if (childComponentList.Count > 0)
            {
                foreach (Equipment e in childComponentList)
                {
                    if (!TestBeforeDeleteComponent(e.Equipment_ID))
                    {
                        isFinded = false;
                    }
                    TestBeforeDeleteChildComponentFinal(e.Equipment_ID);
                }
            }
            return isFinded;
        
        }
       
      


       //  Kiểm tra xem Equipment Code có tồn tại chưa ?
        public static bool IsExits(Equipment equipment)
        {
            Equipment objEquipment = null;
            objEquipment = DB.Equipments.Where(m => (m.CodeEquipment == equipment.CodeEquipment) && (m.Equipment_ID != equipment.Equipment_ID)).FirstOrDefault();
            return objEquipment != null ? true : false;
        }
        public static int GetLastEquipmentID()
        {
            return DB.Equipments.ToList().LastOrDefault().Equipment_ID;
        }
        public static Equipment GetDataByNumber(string number)
        {
            return DB.Equipments.Where(e => e.CodeEquipment == number).FirstOrDefault();
        }
        public static List<Equipment> SearchEquipment(Equipment equip, int shipID)
        {
            var predicate = PredicateBuilder.True<Equipment>();
            predicate = predicate.And(n => n.Ship_ID == shipID);
            if (equip.CodeEquipment != "")
                predicate = predicate.And(n => n.CodeEquipment == equip.CodeEquipment);
            if(equip.NameEquipment != "")
                predicate = predicate.And(n => n.NameEquipment == equip.NameEquipment);
            if(equip.Maker_ID != 0)
                predicate = predicate.And(n => n.Maker_ID == equip.Maker_ID);
            if (equip.Vendor_ID != 0)
                predicate = predicate.And(n => n.Vendor_ID == equip.Vendor_ID);
            if(equip.Serial != "")
                predicate = predicate.And(n => n.Serial == equip.Serial);

            return DB.Equipments.Where(predicate).Cast<Equipment>().ToList();

        }
        public static List<Equipment> PrintStatusOfEquipment(string From, string To, string txtName, int StatusID, int shipID)
        {
            var predicate = PredicateBuilder.True<Equipment>();
            predicate = predicate.And(n => n.Ship_ID == shipID);
            if (From != "")
                predicate = predicate.And(n => n.CodeEquipment.CompareTo(From) >= 0);
            if (To != "")
                predicate = predicate.And(n => n.CodeEquipment.CompareTo(To) <= 0);
            if (txtName != "" && txtName[0] == '$' && txtName[txtName.Length - 1] == '$')
            {
                txtName = txtName.Split('$')[1];
                predicate = predicate.And(n => n.NameEquipment.ToLower().Contains(txtName.ToLower()));
            }
            else
            {
                if (txtName != "")
                {
                    predicate = predicate.And(n => n.NameEquipment.ToLower().CompareTo(txtName.ToLower()) == 0);
                }
            }
            if (StatusID != 0)
                predicate = predicate.And(n => n.Status_ID == StatusID);


            return DB.Equipments.Where(predicate).Cast<Equipment>().ToList();
          
        } 
    }
}
